﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Animator))]
public class Mob : MonoBehaviour
{
    [Header("生物属性设置")]
    [SerializeField]
    private MobHealthData_SO mobData;
    [SerializeField]
    protected MobHealthData_SO data;

    [SerializeField]
    private bool doCreateNewData;

    protected Animator animator;

    public bool IsDead { get; protected set; }

    protected virtual void Awake()
    {
        if (doCreateNewData)
            data = Instantiate<MobHealthData_SO>(mobData);
        else
            data = mobData;
        data.CurrentHealth = data.MaxHealth;
        animator = GetComponent<Animator>();
    }

    public virtual void TakeDamage(int damage, GameObject gObj)
    {
        if (IsDead) return;
        SetAnimationValue("Hurt");
        data.CurrentHealth -= damage;
        Debug.Log(gameObject.name + ":受到" + damage + "伤害");

        if (data.CurrentHealth <= 0)
        {
            data.CurrentHealth = 0;
            Dead();
            return;
        }

        Debug.Log(gameObject.name + ":当前血量" + data.CurrentHealth);
    }

    protected virtual void Dead()
    {
        SetAnimationValue("Dead");
        data.CurrentHealth = 0;
        IsDead = true;
        Debug.Log(gameObject.name + "已死亡");
    }

    protected virtual void SetAnimationValue(string a)
    {
        switch (a)
        {
            case "Hurt":
                animator.SetTrigger("Hurt");
                break;
            case "Dead":
                animator.SetBool("Dead", true);
                break;
            default:
                break;
        }
    }
}
